//
//  BaseCell.m
//  微看
//
//  Created by 吴明磊 on 15/8/19.
//  Copyright © 2015年 wuminglei. All rights reserved.
//

#import "BaseCell.h"
#import "UILabel+Extension.h"
#define Margin 10
#import "HeadLine.h"
#import "UIImage+Compress.h"
#import "UIImageView+WebCache.h"
#import "UIImage+MLEXT.h"
#define CommentTextSize 15

@interface BaseCell ()
// 标签icon
@property(nonatomic,strong)UIImageView *labelIcon;
// 分栏标题
@property(nonatomic,strong)UILabel *subfieldLabel;
// 来源
@property(nonatomic,strong)UILabel *sourceLabel;
// 时间
@property(nonatomic,strong)UILabel *timeLabel;
// 标题
@property(nonatomic,strong)UILabel *titleLabel;
// 图片
@property(nonatomic,strong)UIImageView *contentImage;
// 评论背景
@property(nonatomic,strong)UIView *commentBackView;
// 指标三角
@property(nonatomic,strong)UIImageView *upTrangleImageView;

@property(nonatomic,strong)NSLayoutConstraint *commentBackHeightCons;
@end
@implementation BaseCell

static NSString * const cellIdentifier = @"baseCell";
- (void)awakeFromNib {
   
}

// 获取行高
- (CGFloat)rowHeight {
    return 0;
    
}

//MARK: 自动布局
- (void)layoutSubviews {

    [super layoutSubviews];
    
    [self.labelIcon makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(15);
        make.left.equalTo(self.contentView).offset(Margin);
        make.height.width.offset(Margin);
    }];
    
    [self.subfieldLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.labelIcon);
        make.left.equalTo(self.labelIcon.right).offset(3);
    }];
    
    [self.sourceLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.labelIcon);
        make.left.equalTo(self.subfieldLabel.right).offset(3);
    }];
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.labelIcon);
        make.right.equalTo(self.contentView).offset(-Margin);
    }];
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.labelIcon);
        make.top.equalTo(self.labelIcon.bottom).offset(5);
        make.right.equalTo(self.timeLabel.right).offset(-Margin);
        
        
    }];
    
   [self.contentImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.timeLabel);
        make.top.equalTo(self.titleLabel.bottom).offset(Margin);
        make.height.offset((ScreenSize.width - 2 * Margin) * 0.7);

    }];
    
    [self.upTrangleImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentImage.bottom).offset(Margin);
        make.left.equalTo(self.contentImage).offset(Margin);
        make.width.offset(15);
        make.height.offset(5);
    }];
    
    [self.commentBackView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.upTrangleImageView.bottom);
        make.left.right.equalTo(self.contentImage);
        make.bottom.equalTo(self.contentView).offset(-20);
        
        
    }];
    
}

- (void)addCommentBackHeightNslayoutCons {

    self.commentBackView.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.commentBackHeightCons =  [NSLayoutConstraint constraintWithItem:self.commentBackView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100];
    
    [self.commentBackView addConstraint:self.commentBackHeightCons];
}

//MARK: cell创建添加子控件
+ (instancetype)baseCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{

    BaseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        
        cell = [[BaseCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    return cell;
}

- (nonnull instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self.contentView addSubview:self.labelIcon];
        [self.contentView addSubview:self.subfieldLabel];
        [self.contentView addSubview:self.sourceLabel];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contentImage];
        [self.contentView addSubview:self.upTrangleImageView];
        [self.contentView addSubview:self.commentBackView];
        

        [self addCommentBackHeightNslayoutCons];
     
    }
    
    
    return self;
}

//MARK: 给控件赋值
- (void)setInformation:(HeadLine *)information {

    _information = information;
    
    self.labelIcon.hidden = information.isHot ? NO : YES;
    
    self.subfieldLabel.text = information.channelName;
    
    self.sourceLabel.text = information.from;
    
    self.timeLabel.text = [NSString stringWithFormat:@"%tu",information.createdAt];
    
    self.titleLabel.text = information.title;

    
    self.contentImage.image = [information.imageCache compressImageWithWidth:ScreenSize.width - 2 * Margin];
    
    
    NSLog(@"%tu",information.comments.count);
    
    
    if (information.comments.count == 0) {
        
        self.commentBackHeightCons.constant = 0;
        self.upTrangleImageView.hidden = YES;
        
        [self layoutIfNeeded];
        
    }
    else {
    
        self.upTrangleImageView.hidden = NO;
        
        [self addCommentInBack];
        self.commentBackHeightCons.constant = [self addCommentInBack];
        
        [self layoutIfNeeded];
        
    }
    
    
}

// 在评论面板上添加多条评论
- (CGFloat)addCommentInBack {

    NSInteger count = self.information.comments.count;
    
    NSInteger offsetY = 0;
    
    CGFloat textHeight = 0;
    
    for (int i = 0; i < count; i ++) {
        
        if (i > 3) break; // 最多显示4条评论
        
       Comments *comment =  self.information.comments[i];
        
        
        CGFloat textLength = (comment.body.length + comment.userinfo.uname.length) * CommentTextSize;

        
       CGFloat capacityLength =  (self.commentBackView.bounds.size.width - 2 * Margin);
        
        NSInteger rowCount = textLength / capacityLength == 0 ? 1 : textLength / capacityLength;
        
        UILabel *label = [[UILabel alloc]init];
        
        [self.commentBackView addSubview:label];
        
        NSMutableString *text =comment.userinfo.uname;
        [text appendString:comment.body];
        
        label.text = text;
        
        label.numberOfLines = 0;
        
        CGFloat textY = Margin + offsetY * (Margin + CommentTextSize);
        
        label.frame = CGRectMake(Margin, textY, capacityLength, CommentTextSize);
        
        offsetY += rowCount;
    }
    
    textHeight = offsetY * (Margin + CommentTextSize) + 2 * Margin;
    
    return textHeight;
}



//MARK: 懒加载
- (UIImageView *)labelIcon {

    if (!_labelIcon) {
        
        _labelIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tag_hot"]];
    }
    return _labelIcon;
}

- (UILabel *)subfieldLabel {


    if (!_subfieldLabel) {
        
        _subfieldLabel = [UILabel labelWithTitle:@"" titleColor:[UIColor blueColor] titleSize:9];
    
        [_subfieldLabel sizeToFit];
    }
    return _subfieldLabel;
}
- (UILabel *)sourceLabel {

    if (!_sourceLabel) {

        _sourceLabel = [UILabel labelWithTitle:@"" titleColor:[UIColor lightGrayColor] titleSize:8];
        [_sourceLabel sizeToFit];
    }
    return _sourceLabel;
}
- (UILabel *)timeLabel {

    if (!_timeLabel) {

        _timeLabel = [UILabel labelWithTitle:@"" titleColor:[UIColor lightGrayColor] titleSize:8];
    
        [_timeLabel sizeToFit];
    }
    return _timeLabel;
}
- (UILabel *)titleLabel {

    if (!_titleLabel) {
        
        _titleLabel = [UILabel labelWithTitle:@"" titleColor:[UIColor blackColor] titleSize:13];
        _titleLabel.numberOfLines = 0;
        [_titleLabel sizeToFit];
    }
    return _titleLabel;
}
- (UIImageView *)contentImage {

    if (!_contentImage) {
        _contentImage = [[UIImageView alloc]init];
    }
    return _contentImage;

}
- (UIImageView *)upTrangleImageView {

    if (!_upTrangleImageView) {
        _upTrangleImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"smallTrangle"]];
    }
    return _upTrangleImageView;
}
- (UIView *)commentBackView {
 
    if (!_commentBackView) {
        _commentBackView = [[UIView alloc]init];
        _commentBackView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    }
    return _commentBackView;
}
@end
